﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class SearchPatient : Form
    {
        bool userSelected;
        Patient patient = new Patient();
        public SearchPatient()
        {
            InitializeComponent();
            userSelected = false;
            loadDataGridViewPatient();
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    buttonModify.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "ModifyPatient");
        //    //buttonDelete.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //    buttonDiagnostic.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "NewDiagnosticPatient");
        //    buttonTreatment.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "TreatmentPatient");
        //    //buttonHistorical.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //    buttonPayment.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "SearchPayment");
        //}

        private void loadDataGridViewPatient()
        {
            dataGridViewSearchPatient.AutoGenerateColumns = false;
            dataGridViewSearchPatient.DataSource = PatientManager.LoadDataGridViewPatient();
        }

        private void textBoxSearchPatient_TextChanged(object sender, EventArgs e)
        {
            if (ValidateFields.SearchFieldValidation(textBoxSearchPatient.Text))
            {
                dataGridViewSearchPatient.DataSource = PatientManager.LoadDataGridViewSearchPatient(textBoxSearchPatient.Text);
            }
            else
            {
                dataGridViewSearchPatient.DataSource = null;
            }
        }

        private void dataGridViewSearchPatient_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadDataPatient();
            userSelected = true;
        }

        private void LoadDataPatient()
        {
            textBoxCI.Text = dataGridViewSearchPatient.CurrentRow.Cells[0].Value.ToString();
            textBoxFirstName.Text = dataGridViewSearchPatient.CurrentRow.Cells[1].Value.ToString();
            textBoxLastName.Text = dataGridViewSearchPatient.CurrentRow.Cells[2].Value.ToString();
            textBoxGender.Text = dataGridViewSearchPatient.CurrentRow.Cells[3].Value.ToString();
            textBoxBirthday.Text = dataGridViewSearchPatient.CurrentRow.Cells[4].Value.ToString();
            textBoxPhone.Text = dataGridViewSearchPatient.CurrentRow.Cells[5].Value.ToString();
            textBoxCellPhone.Text = dataGridViewSearchPatient.CurrentRow.Cells[6].Value.ToString();
            textBoxAddress.Text = dataGridViewSearchPatient.CurrentRow.Cells[7].Value.ToString();
            textBoxEmail.Text = dataGridViewSearchPatient.CurrentRow.Cells[8].Value.ToString();
            patient.CIPatient = textBoxCI.Text;
            patient.FirstName = textBoxFirstName.Text;
            patient.LastName = textBoxLastName.Text;
            patient.Gender = textBoxGender.Text;
            patient.Birthday = Convert.ToDateTime(textBoxBirthday.Text);
            patient.Phone = int.Parse(textBoxPhone.Text);
            patient.CellPhone = int.Parse(textBoxCellPhone.Text);
            patient.Address = textBoxAddress.Text;
            patient.Email = textBoxEmail.Text;
            patient.ImagePatient = dataGridViewSearchPatient.CurrentRow.Cells["ImagePatient"].Value.ToString();
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", dataGridViewSearchPatient.CurrentRow.Cells["ImagePatient"].Value.ToString())));
                pictureBoxPicture.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxPicture.Image = picture;
            }

        }

        private void SearchPatient_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (userSelected)
            {
                DialogResult confirm = MessageBox.Show("Are you sure to disable this user?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (confirm.Equals(DialogResult.OK))
                {
                    if (!PatientManager.DisablePatient(textBoxCI.Text))
                    {
                        MessageBox.Show("User enable");
                        this.Close();
                    }
                    loadDataGridViewPatient();
                }
            }
            else
            {
                MessageBox.Show("Error");
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if(userSelected)
            {
                ModifyPatient modifyPatient = new ModifyPatient(patient);
                modifyPatient.ShowDialog();
            }
            else
            {
                MessageBox.Show("Error");
            }
        }

        private void buttonDiagnostic_Click(object sender, EventArgs e)
        {
            if (userSelected)
            {
                NewDiagnosticPatient diagnostic = new NewDiagnosticPatient(patient);
                diagnostic.ShowDialog();
            }
            else
            {
                MessageBox.Show("Error Patient No Select");
            }
        }

        private void buttonTreatment_Click(object sender, EventArgs e)
        {
            if (userSelected)
            {
                if(DiagnosticManager.LoadDataDiagnosticPatient(patient.CIPatient)!= null)
                {
                    TreatmentPatient queryPatient = new TreatmentPatient(patient);
                    queryPatient.ShowDialog();
                }
                else
                {
                    MessageBox.Show("The patient was not carried out any diagnosis");
                }
            }
            else
            {
                MessageBox.Show("Error Patient not select");
            }
        }

        private void buttonPayment_Click(object sender, EventArgs e)
        {
            SearchPayment payment = new SearchPayment(textBoxCI.Text,textBoxFirstName.Text, textBoxLastName.Text);
            payment.ShowDialog();
        }

        private void buttonHistorical_Click(object sender, EventArgs e)
        {

        }

    }
}
